package cn.dazhou.linphonedemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import cn.dazhou.linphonedemo.databinding.ActivityMainBinding
import org.linphone.core.Account
import org.linphone.core.Core
import org.linphone.core.CoreListener
import org.linphone.core.CoreListenerStub
import org.linphone.core.Factory
import org.linphone.core.RegistrationState
import org.linphone.core.TransportType

class MainActivity : AppCompatActivity() {

    private val factory: Factory by lazy { Factory.instance().also { it.setDebugMode(true, "Linphone") } }
    private val core: Core by lazy { factory.createCore(null, null, this@MainActivity) }

    private val coreListener = object: CoreListenerStub() {
        override fun onAccountRegistrationStateChanged(core: Core, account: Account, state: RegistrationState?, message: String) {
            binding.registrationStatus.text = message

            when (state) {
                RegistrationState.Failed, RegistrationState.Cleared -> binding.connect.isEnabled = true
                RegistrationState.Ok -> binding.disconnect.isEnabled = true
                else -> Log.i("ABC", state?.name ?: "null")
            }
        }
    }

    private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.connect.setOnClickListener { login(); it.isEnabled = false }
        binding.disconnect.setOnClickListener { unregister(); it.isEnabled = false }
        binding.delete.setOnClickListener { delete(); it.isEnabled = false }
        binding.coreVersion.text = core.version
    }

    private fun delete() {
        val account = core.defaultAccount ?: return
        core.removeAccount(account)
        core.clearAccounts()
        core.clearAllAuthInfo()
    }

    private fun unregister() {
        val account = core.defaultAccount ?: return
        val params = account.params
        val cloneParams = params.clone()
        cloneParams.registerEnabled = false
        account.params = cloneParams
    }

    private fun login() {
        val username = binding.userName.text.toString()
        val password = binding.password.text.toString()
        val domain = binding.host.text.toString()
        val transport = when (binding.transport.checkedRadioButtonId) {
            R.id.udp -> TransportType.Udp
            R.id.tcp -> TransportType.Tcp
            else -> TransportType.Tls
        }

        val accountParams = core.createAccountParams().apply {
            identityAddress = factory.createAddress("sip:$username@$domain")
            serverAddress = factory.createAddress("sip:$domain")?.also { it.transport = transport }
            registerEnabled = true
        }

        val account = core.createAccount(accountParams)
        val authInfo = factory.createAuthInfo(username, null, password, null, null, domain, null)

        core.addAuthInfo(authInfo)
        core.addAccount(account)
        core.defaultAccount = account
        core.addListener(coreListener)

        account.addListener { account, state, message ->
            Log.i("ABC", "[Account] Registration state changed: $state, $message")
        }

        core.start()
        binding.delete.isEnabled = true
    }
}